from django.utils import unittest
import views, models, myValidators

from interview.models import Question, QuestionSet, QuestionSetList, Rule, Explanation, Fact, Rec
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User

from django.test.client import Client
from django.test.client import RequestFactory
#from django.core.management.commands.dumpdata import models


class InterviewTestCase(unittest.TestCase):
	
	def setUp(self):
		#create question set list
		self.test_set_list = QuestionSetList.objects.create(name="test set list")
		
		#create question sets
		self.test_set = self.test_set_list.question_sets_list.create(name="test set")
		
		#create facts
		self.test_fact1 = Fact.objects.create(name="test fact 1")
		
		#create questions
		self.testq1 = self.test_set.question_sets_for_questions.create(name="test q 1", fact=self.test_fact1, question="this is a test.")
		
		#create recs
		self.test_rec1 = Rec.objects.create(name="test rec 1")
				
		#create rules
		self.test_rule1 = Rule.objects.create(fact1=self.test_fact1, fact2=None, fact3=None, logical_operator1=None, logical_operator2=None, rec1=self.test_rec1, rec2=None)
		
		#create explanations
		self.test_exp1 = Explanation.objects.create(rec=self.test_rec1, human_readable_form="this is a test explanation 1.", detailed_description="this is a test in detail.")
		
	
	
	def test_returnWhen(self):
		fact1 = "always" 
		fact2 = "project_start_up"
		fact3 = ""
		logical_operator1 = "and"
		logical_operator2 = ""
		self.assertEqual(models.returnWhen(fact1, fact2, fact3, logical_operator1, logical_operator2), "always and project_start_up  ")
		
	def test_returnRule(self):
		fact1 = "personal_relationship" 
		fact2 = "project_start_up"
		fact3 = "time_overlap"
		logical_operator1 = "and"
		logical_operator2 = "or"
		rec1 = "face_to_face_meetings"
		rec2 = "sync_comm_strategies"
		self.assertEqual(models.returnRule(fact1, fact2, fact3, logical_operator1, logical_operator2, rec1, rec2), "if personal_relationship and project_start_up or time_overlap :  face_to_face_meetings sync_comm_strategies")
		
	###########	tests for functions in views #############	
		
	def test_models(self):
		self.assertEqual(QuestionSetList.objects.get(pk=1).name, "test set list")
		self.assertEqual(QuestionSet.objects.get(pk=1).name, "test set")
		self.assertEqual(Question.objects.get(pk=1).name, "test q 1")
		self.assertEqual(Rule.objects.get(pk=1).rec1.name, "test rec 1")
		self.assertEqual(Explanation.objects.get(pk=1).rec.name, "test rec 1")
		self.assertEqual(Fact.objects.get(pk=1).name, "test fact 1")
		self.assertEqual(Rec.objects.get(pk=1).name, "test rec 1")
		
	def test_index(self):
		c = Client()
		response = c.get('')
		self.assertEqual(response.context['greetings'], 'Hello there! You can use my system if you want!')
		
	def test_questions(self):
		c = Client()
		response = c.get('/interview/questions/')
		self.assertEqual(response.context['counter'], 1)
		self.assertEqual(response.context['question'].name, "test q 1")
		response = c.get('/interview/questions/2')
		session = c.session
		session.save()
		
	def test_buildListOfAllQuestions(self):
		self.assertEqual(views.buildListOfAllQuestions(self.test_set_list)[0], self.testq1)
		
	def test_go_back_question(self):
		c = Client()
		response = c.get('/interview/questions/')
		self.assertEqual(response.context['counter'], 1)
		self.assertEqual(response.context['question'].name, "test q 1")
		response = c.get('/interview/questions/')
		session = c.session
		session.save()
		response = c.get('/interview/questions/')
		self.assertEqual(response.context['counter'], 1)
		response = c.get('/interview/questions/')
		response = c.get('/interview/questions/1')
		self.assertEqual(response.context['counter'], 1)
		self.assertEqual(response.context['question'].name, "test q 1")
		response = c.get('/interview/questions/')
		session = c.session
		session.save()
		response = c.get('/interview/questions/')
		self.assertEqual(response.context['counter'], 1)
	
	def test_ansers(self):
		c = Client()
		response = c.get('/interview/questions/')
		response = c.get('/interview/answers/', {'fact':response.context['question'].fact})
		session = c.session
		self.assertEqual(session['facts'], ["always", "test fact 1"])
		self.assertEqual(session['recommendations'], ["testrec1"])
		
	def test_returnListOfRulesByRec(self):
		self.assertEqual(views.returnListOfRulesByRec(Rec.objects.get(pk=1))[0].rec1.name, "test rec 1")
		
	def test_ruleContainsThisRec(self):
		self.assertEqual(views.ruleContainsThisRec(Rule.objects.get(pk=1), Rec.objects.get(pk=1)), True)
		
	def test_ruleContainsThisFact(self):
		self.assertEqual(views.ruleContainsThisFact(Rule.objects.get(pk=1), Fact.objects.get(pk=1)), True)
		
	def test_returnQuestionbyFact(self):
		self.assertEqual(views.returnQuestionbyFact(Fact.objects.get(pk=1), Question.objects.all()), (Question.objects.get(pk=1), "Yes"))

	def test_putRuleVarsIntoOriginalRawRule(self):
		self.assertEqual(views.putRuleVarsIntoOriginalRawRule(Rule.objects.get(pk=1)), 'if "test fact 1" : "test rec 1"')
		
	def test_turnRuleIntoCode(self):
		self.assertEqual(views.turnRuleIntoCode('if "project_start_up" and "time_and_cost":    "face_to_face_meetings"  '), 'if "project_start_up" in facts and "time_and_cost" in facts:\n    recommendations.append("face_to_face_meetings")')
		
	###########	tests for functions in myValidators#############
	
	def test_validate_facts_and_recs(self):
		i = 0
		try:
			myValidators. validate_facts_and_recs("my_fact")
			i += 1
			myValidators. validate_facts_and_recs("my  fact   ")
			i += 1
		except:
			pass
		self.assertEqual(i, 1)

	def test_validate_when(self):
		i = 0
		try:
			myValidators.validate_when("blah", "blah", "meh", None, "")
			i += 1
		except:
			pass
		
		self.assertEqual(i, 0)
		
	def test_validate_rule(self):
		i = 0
		try:
			myValidators.validate_rule(None, "blah")
			i += 1
		except:
			pass
		
		self.assertEqual(i, 0)
		
	def test_validate_QuestionSet(self):
		i = 0
		try:
			myValidators.validate_QuestionSet("name", "blah", "blah")
	 		i += 1
 		except:
 		 	pass
 		self.assertEqual(i, 0)

		

